﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Protocal.App.Common;
using 多协议通信平台.Common;
using 多协议通信平台.Entity;

namespace 多协议通信平台.DAL
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions options) : base(options)
        {
            base.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
#if DEBUG
            //启用敏感数据日志记录
            optionsBuilder.EnableSensitiveDataLogging();
            //记录日志          
            optionsBuilder.LogTo(msg =>
            {
                //调试-窗口消息
                System.Diagnostics.Debug.WriteLine(msg);

                //输出-窗口消息
                Console.WriteLine(msg);

            }, Microsoft.Extensions.Logging.LogLevel.Information);
#endif
        }

        public virtual DbSet<Data_LocalRecord> Data_LocalRecords { get; set; }
        public virtual DbSet<Data_ModbusPoint> Data_ModbusPoints { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Data_LocalRecord>(entity =>
            {
                entity.Property(e => e.Avatar).IsRequired(false);
                entity.Property(e => e.Language).IsRequired(false);
                entity.Property(e => e.Theme).IsRequired(false);
            });

            modelBuilder.Entity<Data_ModbusPoint>(entity =>
            {
                entity.Property(e => e.IP).IsRequired(false);
                entity.Property(e => e.Port).IsRequired(false);
                entity.Property(e => e.ComPort).IsRequired(false);
            });
        }
    }
}
